//通过rollup进行打包

//引入相关依赖
import ts from 'rollup-plugin-typescript2';//解析 ts
import json from '@rollup/plugin-json';
import resolvePlugin from '@rollup/plugin-node-resolve';//解析 第三方插件
import path from 'path';//处理路径

//获取文件路径
let packagesDir = path.resolve(__dirname,'packages');

//打包获取需要打包的包  
const packageDir = path.resolve(packagesDir,process.env.TARGET);

const resolve = p=>path.resolve(packageDir,p)
const pkg = require(resolve(`package.json`));
const name = path.basename(packageDir);
//创建一个表
const outputOpions = {
  "esm-bundler":{
    file:resolve(`dist/${name}.esm-bundler.js`),
    format:"es"
  },
  "cjs":{
    file:resolve(`dist/${name}.cjs.js`),
    format:"cjs"
  },
  "global":{
    file:resolve(`dist/${name}.global.js`),
    format:"iife"
  }
}
const option = pkg.buildOptions;

function createConfig(format,output){
  //进行打包
  output.name = option.name;
  output.sourcemap = true;
  return {
    input:resolve('src/index.ts'),//导入
    output,
    plugins:[
      json(),
      ts({//解析ts
        tsconfig:path.resolve(__dirname,"tsconfig.json")
      }),
      resolvePlugin()//解析第三方插件
    ]
  }
}
export default option.formats.map(format=>createConfig(format,outputOpions[format]))